﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Mine
{
    class InterlockedStation: IStation
    {

        private long stones, wagon;
           private int id;


        public void Initialize(int wagon, int stones)
        {
            this.wagon = wagon;
            this.stones = stones;
        }


        public int Stones
        {
            get { return (int)stones; }
            set
            {
                stones = value;
            }
        }

        public int Wagon
        {
            get
            {
                return (int)wagon;
            }
            set
            {
                wagon = value;
            }
        }



        public int ID
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }

        public double GetConsumption(long Stones)
        {
            return Math.Sqrt(Math.Pow(Math.Sqrt(Stones), Math.PI));
        }

        public  double MoveStones(int Amount, IStation destination)
        {
            InterlockedStation Destination= (InterlockedStation)destination;
            double r = GetConsumption(stones);
            //Interlocked.Add Adds two 32-bit integers and replaces the first integer with the sum, as an atomic operation.
            Interlocked.Add(ref stones,-Amount);
            Interlocked.Add(ref Destination.stones,Amount);
            Interlocked.Exchange(ref Destination.wagon, Interlocked.Exchange(ref wagon,Destination.wagon));//kicserereli a 2 wagont
            //Interlocked.Exchange Sets a 32-bit signed integer to a specified value and returns the original value, as an atomic operation.
            return r;
        }
    }
}
